<?php
// php 实现二分查找
// 1, 2, 4, 7, 9, 11
function binarySearch($arr, $key, $start, $end)
{
    if ($start > $end) {
        return -1;
    }
    $mid = $start + ($end - $start) / 2;
    if ($arr[$mid] == $key) {
        return $mid;
    } else {
        if ($arr[$mid] < $key) {
            return binarySearch($arr, $key, $mid + 1, $end);
        } else {
            return binarySearch($arr, $key, $start, $mid - 1);
        }
    }
}
$arr = [1, 2 ,4, 7, 9, 11];
$key = 9;
echo binarySearch($arr, $key, 0,count($arr));
// 获取变量类型gettype
var_dump(gettype($arr));
var_dump(is_array($arr));
